<script setup lang="ts">
// +++++++++++++++++++++++++++++++++++++++++++++++
// ---------- 模块导入 ----------
// +++++++++++++++++++++++++++++++++++++++++++++++

import { onMounted, shallowRef } from 'vue';
import { $t } from '@/locales';
import { useDict } from '@/hooks/business/dict';
import { fetchGetCjProjectOptions } from '@/service/api';

// +++++++++++++++++++++++++++++++++++++++++++++++
// ---------- 组件名称 ----------
// +++++++++++++++++++++++++++++++++++++++++++++++

defineOptions({
  name: 'CjRecordSearch'
});

// +++++++++++++++++++++++++++++++++++++++++++++++
// ---------- 组合式函数 ----------
// +++++++++++++++++++++++++++++++++++++++++++++++

const { dictOptions } = useDict();

// +++++++++++++++++++++++++++++++++++++++++++++++
// ---------- 父子传参 ----------
// +++++++++++++++++++++++++++++++++++++++++++++++

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

// +++++++++++++++++++++++++++++++++++++++++++++++
// ---------- 框架模型 ----------
// +++++++++++++++++++++++++++++++++++++++++++++++

const model = defineModel<Api.Sampling.RecordSearchParams>('model', { required: true });

// +++++++++++++++++++++++++++++++++++++++++++++++
// ---------- 业务模型 ----------
// +++++++++++++++++++++++++++++++++++++++++++++++

const projectOptions = shallowRef<CommonType.Option[]>([]);

// +++++++++++++++++++++++++++++++++++++++++++++++
// ---------- 框架方法 ----------
// +++++++++++++++++++++++++++++++++++++++++++++++

function reset() {
  emit('reset');
}

function search() {
  emit('search');
}

// +++++++++++++++++++++++++++++++++++++++++++++++
// ---------- 业务方法 ----------
// +++++++++++++++++++++++++++++++++++++++++++++++

// 处理采样项目下拉列表
async function initAllProjectOptions() {
  const { error, data } = await fetchGetCjProjectOptions();
  if (!error && data) {
    projectOptions.value = data;
    projectOptions.value.splice(0, 0, {
      value: '-1',
      label: '未关联项目'
    });
  }
}

// +++++++++++++++++++++++++++++++++++++++++++++++
// ---------- 框架生命周期 ----------
// +++++++++++++++++++++++++++++++++++++++++++++++

onMounted(() => {
  initAllProjectOptions();
});

// +++++++++++++++++++++++++++++++++++++++++++++++
// ---------- 业务生命周期 ----------
// +++++++++++++++++++++++++++++++++++++++++++++++
</script>

<template>
  <NCard :bordered="false" size="small" class="card-wrapper">
    <NForm :model="model" label-placement="left" :show-feedback="false" :label-width="80">
      <NGrid responsive="screen" item-responsive :x-gap="8" :y-gap="8" cols="1 s:1 m:5 l:5 xl:5 2xl:5">
        <NGridItem span="4">
          <NGrid responsive="screen" item-responsive :x-gap="8">
            <NFormItemGi span="24 s:8 m:6" :label="$t('page.sampling.task.projectId')" path="projectId">
              <NSelect v-model:value="model.projectId" size="small" :options="projectOptions" class="mb-12px w-300px" clearable></NSelect>
            </NFormItemGi>
            <NFormItemGi span="24 s:8 m:6" :label="$t('page.sampling.task.year')" path="year">
              <NInput v-model:value="model.year" size="small" clearable />
            </NFormItemGi>
            <NFormItemGi span="24 s:8 m:6" :label="$t('page.sampling.task.samplingType')" path="samplingType">
              <NSelect
                v-model:value="model.samplingType"
                size="small"
                :options="dictOptions('cj_sampling_type')"
                class="mb-12px w-300px"
                clearable
              ></NSelect>
            </NFormItemGi>
            <NFormItemGi span="24 s:8 m:6" :label="$t('page.sampling.task.taskStatus')" path="taskStatus">
              <NSelect
                v-model:value="model.taskStatus"
                size="small"
                :options="dictOptions('cj_task_status')"
                class="mb-12px w-300px"
                clearable
              ></NSelect>
            </NFormItemGi>
            <NFormItemGi span="24 s:8 m:6" :label="$t('page.sampling.task.province')" path="provinceName">
              <NInput v-model:value="model.provinceName" size="small" clearable />
            </NFormItemGi>
            <NFormItemGi span="24 s:8 m:6" :label="$t('page.sampling.task.city')" path="cityName">
              <NInput v-model:value="model.cityName" size="small" clearable />
            </NFormItemGi>
            <NFormItemGi span="24 s:8 m:6" :label="$t('page.sampling.task.county')" path="countyName">
              <NInput v-model:value="model.countyName" size="small" clearable />
            </NFormItemGi>
            <NFormItemGi span="24 s:8 m:6" :label="$t('page.sampling.task.town')" path="town">
              <NInput v-model:value="model.town" size="small" clearable />
            </NFormItemGi>
          </NGrid>
        </NGridItem>
        <NGridItem>
          <NFormItemGi span="24 s:8 m:6">
            <NSpace class="w-full" justify="end">
              <NButton type="primary" ghost @click="search">
                <template #icon>
                  <icon-ic-round-search class="text-icon" />
                </template>
                {{ $t('common.search') }}
              </NButton>
              <NButton quaternary @click="reset">
                <template #icon>
                  <icon-ic-round-refresh class="text-icon" />
                </template>
                {{ $t('common.reset') }}
              </NButton>
            </NSpace>
          </NFormItemGi>
        </NGridItem>
      </NGrid>
    </NForm>
  </NCard>
</template>

<style scoped></style>
